/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */








//Inicia Trata Mascaras de Campos
function Mascara(objeto, evt, mascara) {
    var LetrasU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var LetrasL = 'abcdefghijklmnopqrstuvwxyz';
    var Letras  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    var Numeros = '0123456789';
    var Fixos  = '().,-:/ ';
    var Charset = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_/`abcdefghijklmnopqrstuvwxyz{|}~";

    evt = (evt) ? evt : (window.event) ? window.event : "";

    var value = objeto.value;
 
    if (evt) {
        var ntecla = (evt.which) ? evt.which : evt.keyCode;
        tecla = Charset.substr(ntecla - 32, 1);
//        if (ntecla == 13) return false;
//        if (ntecla < 32) return true;
        if (ntecla == 13) return false;
        if (ntecla <  32) return true;
        if (ntecla == 37) return true;
        if (ntecla == 46) return true;
        if (ntecla == 39) return true;
        var tamanho = value.length;
        if (tamanho >= mascara.length) return false;

        var pos = mascara.substr(tamanho,1);

        while (Fixos.indexOf(pos) != -1) {
            value += pos;
            tamanho = value.length;
            if (tamanho >= mascara.length) return false;
            pos = mascara.substr(tamanho,1);
        }

        switch (pos) {
            case '#' : if (Numeros.indexOf(tecla) == -1) return false; break;
            case 'A' : if (LetrasU.indexOf(tecla) == -1) return true; break;
            case 'a' : if (LetrasL.indexOf(tecla) == -1) return false; break;
            case 'Z' : if (Letras.indexOf(tecla) == -1) return false; break;
            case '*' : objeto.value = value; return true; break;
            default : return false; break;
        }
    }
    objeto.value = value;
    return true;
}


function MascaraKilo(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';

    var whichCode = (e.which) ? e.which : e.keyCode;//(window.Event) ? e.which : e.keyCode;
        
    
    if (whichCode == 9) return true;
    if (whichCode == 8) return true;
    
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;

    len = aux.length;
    // window.alert(len);
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal +'0'+aux;
    if (len == 3) objTextBox.value =  '0'+SeparadorDecimal + aux;


    //window.alert(objTextBox.value)
    if (len > 3) {
        aux2 = '';
        for (j = 0, i = len - 4; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);

            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;

        //window.alert("* "+len2)


        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        //window.alert(aux)
        objTextBox.value += SeparadorDecimal + aux.substr(len - 3, len);

    }

    return false;
}

function MascaraItem(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';

    var whichCode = (e.which) ? e.which : e.keyCode;//(window.Event) ? e.which : e.keyCode;

    if (whichCode == 9) return true;
    if (whichCode == 8) return true;
    if (whichCode == 37) return true;
    if (whichCode == 39) return true;
    if (whichCode == 13) return true;
    if (whichCode == 46) return true;
    


    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    
    if(whichCode>=96 && whichCode<=105){
        switch(whichCode){
            case 96:
                key='';
                key=0;
                break;
            case 97:
                key='';
                key=1;
                break;
            case 98:
                key='';
                key=2;
                break;
            case 99:
                key='';
                key=3;
                break;
            case 100:
                key='';
                key=4;
                break;
            case 101:
                key='';
                key=5;
                break;
            case 102:
                key='';
                key=6;
                break;
            case 103:
                key='';
                key=7;
                break;
            case 104:
                key='';
                key=8;
                break;
            case 105:
                key='';
                key=9;
                break;
   
        }     
    }

    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
    
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    (objTextBox.value.charAt(i))
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    

    len = aux.length;
    // window.alert(len);
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal +'0'+aux;
    if (len == 3) objTextBox.value = '0'+ SeparadorDecimal + aux;


    //window.alert(objTextBox.value)
    if (len > 3) {
        aux2 = '';
        for (j = 0, i = len - 4; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);

            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;

        //window.alert("* "+len2)


        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        //window.alert(aux)
        objTextBox.value += SeparadorDecimal + aux.substr(len - 3, len);

    }

    return false;
}
//essa mascara é usada no evento onkeydown 
function MascaraPreco4DigKeyDown(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';

    var whichCode = (e.which) ? e.which : e.keyCode;//(window.Event) ? e.which : e.keyCode;
    if (whichCode == 9) return true;
    if (whichCode == 8) return true;
    if (whichCode == 37) return true;
    if (whichCode == 39) return true;
    if (whichCode == 46) return true;
    if (whichCode == 13) return true;

    key = String.fromCharCode(whichCode); // Valor para o código da Chave
   
    if(whichCode>=96 && whichCode<=105){
        switch(whichCode){
            case 96:
                key='';
                key=0;
                break;
            case 97:
                key='';
                key=1;
                break;
            case 98:
                key='';
                key=2;
                break;
            case 99:
                key='';
                key=3;
                break;
            case 100:
                key='';
                key=4;
                break;
            case 101:
                key='';
                key=5;
                break;
            case 102:
                key='';
                key=6;
                break;
            case 103:
                key='';
                key=7;
                break;
            case 104:
                key='';
                key=8;
                break;
            case 105:
                key='';
                key=9;
                break;

        }
    }

    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;

    len = aux.length;
    // window.alert(len);
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal +'0'+aux;
    if (len == 3) objTextBox.value = '0'+SeparadorDecimal + aux;
    if (len == 4) objTextBox.value = '0'+SeparadorDecimal + aux;


    //window.alert(objTextBox.value)
    if (len > 4) {
        aux2 = '';
        for (j = 0, i = len - 5; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);

            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;

        //window.alert("* "+len2)

        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        //window.alert(aux)
        objTextBox.value += SeparadorDecimal + aux.substr(len - 4, len);

    }

    return false;
}

///mascara kilo para keydown
function MascaraKiloKeyDown(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (e.which) ? e.which : e.keyCode;


    if (whichCode == 13) return true;
    if (whichCode == 37) return true;
    if (whichCode == 39) return true;
    if (whichCode == 9)  return true;
    if (whichCode == 8)  return true;
    if (whichCode == 37) return true;
    if (whichCode == 39) return true;
    if (whichCode == 46) return true;


    key = String.fromCharCode(whichCode); // Valor para o código da Chave

    if(whichCode>=96 && whichCode<=105){
        switch(whichCode){
            case 96:
                key='';
                key=0;
                break;
            case 97:
                key='';
                key=1;
                break;
            case 98:
                key='';
                key=2;
                break;
            case 99:
                key='';
                key=3;
                break;
            case 100:
                key='';
                key=4;
                break;
            case 101:
                key='';
                key=5;
                break;
            case 102:
                key='';
                key=6;
                break;
            case 103:
                key='';
                key=7;
                break;
            case 104:
                key='';
                key=8;
                break;
            case 105:
                key='';
                key=9;
                break;

        }
    }

    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal +'0'+aux;
    if (len == 3) objTextBox.value = '0'+SeparadorDecimal + aux;

    if (len > 3) {
        aux2 = '';
        for (j = 0, i = len - 4; i >= 0; i--) {
            if (j == 2) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 3, len);
    }
    return false;
}



//essa mascara é usada no evento onkeydown
function MascaraMoedaKeyDown(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (e.which) ? e.which : e.keyCode;


    if (whichCode == 13) return true;
    if (whichCode == 37) return true;
    if (whichCode == 39) return true;
    if (whichCode == 9)  return true;
    if (whichCode == 8)  return true;
    if (whichCode == 37) return true;
    if (whichCode == 39) return true;
    if (whichCode == 46) return true;
    
    
    key = String.fromCharCode(whichCode); // Valor para o código da Chave

    if(whichCode>=96 && whichCode<=105){
        switch(whichCode){
            case 96:
                key='';
                key=0;
                break;
            case 97:
                key='';
                key=1;
                break;
            case 98:
                key='';
                key=2;
                break;
            case 99:
                key='';
                key=3;
                break;
            case 100:
                key='';
                key=4;
                break;
            case 101:
                key='';
                key=5;
                break;
            case 102:
                key='';
                key=6;
                break;
            case 103:
                key='';
                key=7;
                break;
            case 104:
                key='';
                key=8;
                break;
            case 105:
                key='';
                key=9;
                break;

        }
    }

    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
   var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    //window.alert(SeparadorMilesimo+" / "+SeparadorDecimal+" / ")
    var whichCode = (e.which) ? e.which : e.keyCode;//(window.Event) ? e.which : e.keyCode;
    if (whichCode == 9) return true;
    if (whichCode == 8) return true;

    
    


    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;

    len = aux.length;
    // window.alert(len);
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal +'0'+aux;



    //window.alert(objTextBox.value)
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) { 
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);

            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;

        //window.alert("* "+len2)

        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        //window.alert(aux)
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);

    }
    return false;
}


//essa mascara mostra as casas decimais no momento esta usando 4 casas decimais
function MascaraPreco(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    //window.alert(SeparadorMilesimo+" / "+SeparadorDecimal+" / ")
    var whichCode = (e.which) ? e.which : e.keyCode;//(window.Event) ? e.which : e.keyCode;
    if (whichCode == 9) return true;
    if (whichCode == 8) return true;

    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
   
    len = aux.length;
    // window.alert(len);
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal +'0'+aux;
    if (len == 3) objTextBox.value = '0'+ SeparadorDecimal +'0'+ aux; //alteracao para 4 casas decimais
    if (len == 4) objTextBox.value = '0'+SeparadorDecimal + aux; //alteracao para 4 casas decimais
    
  
    //window.alert(objTextBox.value)
    if (len > 4) {//alteracao para 4 casas decimais
        aux2 = '';
        for (j = 0, i = len - 5; i >= 0; i--) { //alteracao para 4 casas decimais
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;

        //window.alert("* "+len2)

        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        //window.alert(aux)
        objTextBox.value += SeparadorDecimal + aux.substr(len - 4, len);//alteracao para 4 casas decimais
       
    }

    return false;
}


//Mascara Preco 4 digitos
/*
function MascaraPreco4Dig(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
  
    var whichCode = (e.which) ? e.which : e.keyCode;//(window.Event) ? e.which : e.keyCode;
    if (whichCode == 9) return true;
    if (whichCode == 8) return true;
  
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;

    len = aux.length;
    // window.alert(len);
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal +'0'+aux;
    if (len == 3) objTextBox.value = '0'+SeparadorDecimal + aux;
    if (len == 4) objTextBox.value = '0'+SeparadorDecimal + aux;


    //window.alert(objTextBox.value)
    if (len > 4) {
        aux2 = '';
        for (j = 0, i = len - 5; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);

            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;

        //window.alert("* "+len2)

        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        //window.alert(aux)
        objTextBox.value += SeparadorDecimal + aux.substr(len - 4, len);

    }

    return false;
}*/


function mascaraCEP(objeto, evt) {
    return Mascara(objeto, evt, '#####-###');
}

function mascaraFone(objeto, evt) {
    return Mascara(objeto, evt, '(##)####-####');
}

var tipo;
function mascaraCPF(objeto, evt) {
    return Mascara(objeto, evt,'###.###.###-##');
}

function mascaraCNPJ(objeto, evt) {
    //Mascara(objeto, evt, '##.###.###/####-##')
    //FormataCNPJ(objeto, evt);
    return Mascara(objeto, evt, '##.###.###/####-##') ;
}
function mascaraRG(objeto, evt) {
    return Mascara(objeto, evt, '#.###.###-#');
}
function mascaraPlacaCarro(objeto, evt) {
    return Mascara(objeto, evt, 'AAA-####');
}

function mascaraData(objeto, evt) {
    return Mascara(objeto, evt, '##/##/####');
}


function mascaraHora(objeto, evt) {
    return Mascara(objeto, evt, '##:##');
}

function mascaraUf(objeto, evt) {
    return Mascara(objeto, evt, 'aa');
}



function mascaraSomenteInteger(obj,even){
    
    if(even) {
        
        var Charset = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_/`abcdefghijklmnopqrstuvwxyz{|}~";
        var Nums = '0123456789';
        var valor = "";

        even = (even) ? even : (window.event) ? window.event : "";
        var ntecla = (even.which) ? even.which : even.keyCode;

        tecla = Charset.substr(ntecla - 32, 1);
        

        if(tecla == "." || tecla=="," )return true;

        
        
        if(ntecla == 108){
            obj.value= "";
            return false;
        }

        
 
        v = obj.value.replace(/[^-0123456789]/g, "");

        if(ntecla < 13) return true;
        
        if(Nums.indexOf(tecla) == -1) return false;
              
        if(tecla == String.fromCharCode(45) && v.length > 0){
            return false;
        }
        
    
    
    }
    
    return true;
}

function mascaraPorcentagem(obj,even){

    if(even) {
        var Charset = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_/`abcdefghijklmnopqrstuvwxyz{|}~";
        var Nums = '0123456789';
        var valor = "";

        even = (even) ? even : (window.event) ? window.event : "";
        var ntecla = (even.which) ? even.which : even.keyCode;

        tecla = Charset.substr(ntecla - 32, 1);
        if(tecla == "." || tecla=="," )return true;

        if(ntecla == 108){
            obj.value= "";
            return false;
        }
        if (ntecla == 37) return true;
        if (ntecla == 39) return true;
        if (ntecla == 13) return true;

        if(ntecla == 96||ntecla==97||ntecla==98||ntecla==99||ntecla==100||ntecla==101||ntecla==102||ntecla==103||ntecla==104||ntecla==105|| ntecla==110)return true;
        v = obj.value.replace(/[^-0123456789]/g, "");

        if(ntecla < 13) return true;
        if(Nums.indexOf(tecla) == -1) return false;

        if(tecla == String.fromCharCode(45) && v.length > 0){
            return false;
        }
    }
    return true;
}

function mascaraNumero(obj,even,dec){
    if(even) {
        var Charset = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_/`abcdefghijklmnopqrstuvwxyz{|}~";
        var Nums = '0123456789';
        var valor = "";
        even = (even) ? even : (window.event) ? window.event : "";
        var ntecla = (even.which) ? even.which : even.keyCode;       
        tecla = Charset.substr(ntecla - 32, 1);
        if(ntecla == 108){
            obj.value= "";
            return false;
        }
        v = obj.value.replace(/[^-0123456789]/g, "");

        if(ntecla < 32) return true;
        if(Nums.indexOf(tecla) == -1) return false;

        if(tecla == String.fromCharCode(45) && v.length > 0){
            return false;
        }
        //
        if((v.length + 1) > dec){
            var valorInt = v.substring(0,((v.length + 1)  - dec));
            var valorDec = "." + v.substring(((v.length + 1)  - dec),v.length);
            if(valorInt.length > 3){
                var resto = valorInt.length % 3;
                var valorResto = valorInt.substring(0,resto);

                val = "";
                for(i = resto;i < valorInt.length; i+=3){
                    if(i != 0){
                        val += ".";
                    }
                    val += valorInt.substring(i,(i+3));
                }
                if(Left(v,1) == "-" ){
                    if(resto == 1){
                        val= Right(val,val.length-1);
                    }
                }
                valorInt = valorResto + val;
            }
            valor += valorInt + valorDec;
        }else{
            valor += ".";
            for(i=0; i < (dec - (v.length + 1));i++){
                valor += "";
            }
            valor += v;
        }
        obj.value = valor;
    }
    return true;
}





function mostraEndereco(){
    alert("...")
    var map = null;
    var geocoder = null;
    var endereco = document.getElementById("endereco").value;
    window.alert("---" + endereco);
    if (GBrowserIsCompatible()) {
        alert("aaa");
        map = new GMap2(document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:gMap12"));

        geocoder = new GClientGeocoder();
        alert(geocoder);
    }
    if ( geocoder ) {
        geocoder.getLatLng(endereco,
            function(point){
                if ( !point ) {
                    alert(endereco + " não encontrado");
                } else {
                    map.setCenter(point, 13);
                    var marca = new GMarker(point);
                    map.addOverlay(marca);
                    marca.openInfoWindowHtml( endereco + "<br />" + point.toString() );
                }
            }
            );
    } else {
        alert("GeoCoder não identificado");
    }
}

var myEventListener;
var gmap = null;
var geocoder = null;

function criarMarcador(){
    //   alert(gmap);
    //  gmap.setCenter(new GLatLng(-23.552228, -51.472127), 16);
    if (gmap == null){
        gmap = new GMap2(document.getElementById("formg:Frag_GMap:gmap"));
        geocoder = new GClientGeocoder();
        var latit = document.getElementById("formg:Frag_GMap:latCentral").value;
        var longit = document.getElementById("formg:Frag_GMap:longCentral").value;
        var ponto = new GLatLng(latit, longit);
        gmap.setCenter(ponto, 17);
    }
    gmap.setMapType(G_HYBRID_MAP);
    
    myEventListener = GEvent.addListener(gmap,"click", function(overlay, latlng) {
        gmap.clearOverlays();
        if (latlng) {            
            //        gmap.openInfoWindow(latlng, myHtml);
            var lat = latlng.lat();
            var lon = latlng.lng();
            document.getElementById("formg:Frag_GMap:latitude").value = lat;
            document.getElementById("formg:Frag_GMap:longitude").value = lon;
            var point = new GLatLng(lat, lon);
            var marca = new GMarker(point);
            gmap.addOverlay(marca);
            gmap.setCenter(point, gmap.getZoom());
        //    var myHtml = "Ultimo ponto selecionado.";
        //     gmap.openInfoWindow(latlng, myHtml);
        }
    });
}





function teste(){
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);

    GEvent.addListener(map,"click", function(overlay, latlng) {
        if (latlng) {
            var myHtml = "The GPoint value is: " + map.fromLatLngToDivPixel(latlng) + " at zoom level " + map.getZoom();
            map.openInfoWindow(latlng, myHtml);
        }
    });
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
}

function retiraListener(){
    if (gmap != null){
        gmap.clearOverlays();
        GEvent.removeListener(myEventListener);
    } 
}

function iniciar(){
    alert("iniciei");
    var lati = document.getElementById("formg:Frag_GMap:latitude").value;
    var longi = document.getElementById("formg:Frag_GMap:longitude").value;
    if ((lati == "null") || (lati == "") || (lati == 0)){
        geocoder.getLatLng(
            address,
            function(point) {
                if (!point) {
                    alert("Endereço " + address + " não foi encontrado!");
                } else {
                    gmap.setCenter(point, 16);
                    var marker = new GMarker(point);
                    gmap.addOverlay(marker);
                    marker.openInfoWindowHtml(address);
                }
            }
            );
    }else{
        var point = new GLatLng(lati, longi);
        var marker = new GMarker(point);
        gmap.addOverlay(marker);
        marker.openInfoWindowHtml("Geo Localização <br/>" + address);
        gmap.setMapType(G_HYBRID_MAP);
        gmap.setCenter(point, 17);
    }
    gmap.addControl(new GLargeMapControl());
    gmap.addControl(new GMapTypeControl());
}

var enderecof;
var latitudef;
var longitudef;
function showAddressPopUp(address, lati, longi) {
    // if (executa == "n"){
    enderecof=address;
    latitudef=lati;
    longitudef=longi;
    gmap = new GMap2(document.getElementById("formg:Frag_GMap:gmap"));
    geocoder = new GClientGeocoder();
    gmap.clearOverlays();
    document.getElementById("formg:Frag_GMap:latitude").value = lati;
    document.getElementById("formg:Frag_GMap:longitude").value = longi;
    //   alert(lat);
    if ((lati == "null") || (lati == "") || (lati == 0)){
        geocoder.getLatLng(
            address,
            function(point) {
                if (!point) {
                    alert("Endereço " + address + " não foi encontrado!");
                } else {
                    gmap.setCenter(point, 16);
                    var marker = new GMarker(point);
                    gmap.addOverlay(marker);
                    marker.openInfoWindowHtml(address);
                }
            }
            );
    }else{
        var point = new GLatLng(lati, longi);
        var marker = new GMarker(point);
        gmap.addOverlay(marker);
        marker.openInfoWindowHtml("Geo Localização <br/>" + address);
        gmap.setMapType(G_HYBRID_MAP);
        gmap.setCenter(point, 17);
    }
    gmap.addControl(new GLargeMapControl());
    gmap.addControl(new GMapTypeControl());
//    }
}

function showAddress(address) {
    gmap = new GMap2(document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:gMap12"));
    geocoder = new GClientGeocoder();
    geocoder.getLatLng(
        address,
        function(point) {
            if (!point) {
                alert("Endereço " + address + " não foi encontrado!");
            } else {
                gmap.setCenter(point, 16);
                var marker = new GMarker(point);
                gmap.addOverlay(marker);
                alert(point.getPostalCode());
                marker.openInfoWindowHtml(address);
            }
        }

        );
    if (clickEventListener != null){
        GEvent.removeListener(clickEventListener);
    }
    clickEventListener = GEvent.addListener(gmap, "click", clicked);
    gmap.addControl(new GLargeMapControl());
    gmap.addControl(new GMapTypeControl());
    gmap.setZoom(18);
}

var clickEventListener = null;

function inicializa(){
    alert(document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:executouBotaoTela").value);
    if (document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:executouBotaoTela").value == "n"){
        if (GBrowserIsCompatible()) {
            showAddress("Rua Ponta grossa, Apucarana");
        }
    }

//    geocoder = new GClientGeocoder();
//    gmap = new GMap2(document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:gMap12"));
//        //        alert("inicializa");
//        //        gmap = new GMap2(document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:gMap12"));
//        //        geocoder = new GClientGeocoder();
//        var executouBotaoTela = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:executouBotaoTela").value;
//        alert("entrrei aqui");
//        if (executouBotaoTela == "n"){
//            showAddress("R: Antonio Jose de Oliveira, Apucarana, Parana, Brasil");
//        }else{
//            var flagCep = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:flagCep").value;
//            if (flagCep == "n"){
//                alert("entre aqui no endereco");
//
//            var pais = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:soPais").value;
//            var estado = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:soEstado").value;
//            var cidade = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:soCidade").value;
//            var endereco = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:itEndereco").value;
//               showAddress(endereco + ", " + cidade + ", " + estado + ", " + pais);
//            }else{
//
//               var cep = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:itCep").value;
//               alert("executando cep" + cep);
//               showAddress(cep);
//            }
//            document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:executouBotaoTela").value = "n";
//        }
//
//    //
//    //        gmap.addControl(new GLargeMapControl());
//    //        gmap.addControl(new GMapTypeControl());
//    //        gmap.setZoom(18);
//
    
}

//function buscaEnderecoPeloBotao(){
//    if (GBrowserIsCompatible()) {
//        var pais = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:soPais").value;
//        var estado = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:soEstado").value;
//        var cidade = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:soCidade").value;
//        var endereco = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:itEndereco").value;
//        showAddress(endereco + ", " + cidade + ", " + estado + ", " + pais);
//    }
//}

function buscaEndereco(){
    if (GBrowserIsCompatible()) {
        //    if (gmap == null){
        //        gmap = new GMap2(document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:gMap12"));
        //        geocoder = new GClientGeocoder();
        //    }
        var pais = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:soPais").value;
        var estado = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:soEstado").value;
        var cidade = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:soCidade").value;
        var endereco = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:itEndereco").value;
        showAddress(endereco + ", " + cidade + ", " + estado + ", " + pais);
    }
}

function buscaCep(){
    if (GBrowserIsCompatible()) {
        //    if (gmap == null){
        //        gmap = new GMap2(document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:gMap12"));
        //        geocoder = new GClientGeocoder();
        //    }
        var cep = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:itCep").value;
        showAddress(cep);
    }
}

function adicionarMarcadores(){
    // Create a base icon for all of our markers that specifies the
    // shadow, icon dimensions, etc.
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);

    // Creates a marker whose info window displays the letter corresponding
    // to the given index.
    function createMarker(point, index) {
        // Create a lettered icon for this point using our icon class
        var letter = String.fromCharCode("A".charCodeAt(0) + index);
        var letteredIcon = new GIcon(baseIcon);
        letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

        // Set up our GMarkerOptions object
        markerOptions = {
            icon:letteredIcon
        };
        var marker = new GMarker(point, markerOptions);

        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml("Marker <b>" + letter + "</b>");
        });
        return marker;
    }

    // Add 10 markers to the map at random locations
    var bounds = gmap.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    var lngSpan = northEast.lng() - southWest.lng();
    var latSpan = northEast.lat() - southWest.lat();
    for (var i = 0; i < 10; i++) {
        var latlng = new GLatLng(southWest.lat() + latSpan * Math.random(),
            southWest.lng() + lngSpan * Math.random());
        gmap.addOverlay(createMarker(latlng, i));
    }

}

/*

var iconBlue = new GIcon();
iconBlue.image = './resources/mm_20_blue.png';
iconBlue.shadow = './resources/mm_20_shadow.png';
iconBlue.iconSize = new GSize(12, 20);
iconBlue.shadowSize = new GSize(22, 20);
iconBlue.iconAnchor = new GPoint(6, 20);
iconBlue.infoWindowAnchor = new GPoint(5, 1);

var iconRed = new GIcon();
iconRed.image = './resources/mm_20_red.png';
iconRed.shadow = './resources/mm_20_shadow.png';
iconRed.iconSize = new GSize(12, 20);
iconRed.shadowSize = new GSize(22, 20);
iconRed.iconAnchor = new GPoint(6, 20);
iconRed.infoWindowAnchor = new GPoint(5, 1);

var customIcons = [];
customIcons["'Empresa'"] = iconRed;
customIcons["'bar'"] = iconBlue;

*/
// escolher tamanho do icone e a cor
// var newIcon = MapIconMaker.createMarkerIcon({width: 44, height: 44, primaryColor: "#00ff00"});
// var marker = new GMarker(stores[0].latlng, {icon: newIcon});




//function testeqw(){
//    geocoder = new GClientGeocoder();
//    gmap = new GMap2(document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:gMap12"));
//}

function carregaArquivo(){
    //    geocoder = new GClientGeocoder();
    //    gmap = new GMap2(document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:gMap12"));
    gmap.clearOverlays();
    var numero = document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:numeroArquivo").value;
    GDownloadUrl("./xml/output.xml", function(data) {
        document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:numeroArquivo").value = numero;
        alert(data);
        var xml = GXml.parse(data);
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
            
            var name = markers[i].getAttribute("name");
            var address = markers[i].getAttribute("address");
            var type = markers[i].getAttribute("type");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                parseFloat(markers[i].getAttribute("lng")));
            var marker = createMarker(point, name, address, type);
            
            gmap.addOverlay(marker);
        }
    });
}

function createMarker(point, name, address, type) {
    var marker = new GMarker(point, customIcons[type]);
    var html = "<b>" + name + "</b> <br/>" + address;
    GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
        gmap.setCenter(point, gmap.getZoom());
    });
    return marker;
}

//Inibe o uso de qualquer caracter que não seja letras, sem qualquer acentuação
function caractersOnly(evt) {
    var browser;
    browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer") {
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
        if ((charCode > 32 && charCode < 65 ) || (charCode > 90 && charCode < 97) || (charCode > 122 && charCode < 127) || (charCode > 127)){
            return false;
        }
    } else {
        evt = window.event ? evt.keyCode : evt.which;
        var charCode = evt;
        if ((charCode > 32 && charCode < 65 ) || (charCode > 90 && charCode < 97) || (charCode > 122 && charCode < 127) || (charCode > 127)){
            return false;
        }
    }
    return true;
}

function adicionaRetiraListenerMarcador(val){
    //alert(val.value);
    var valor = document.getElementById("formg:Frag_GMap:selectOneRadioListener").value;
    alert(valor);
    if (valor == "true"){
        alert(gmap);
        gmap.setCenter(new GLatLng(-23.552228, -51.472127), 16);
        gmap.setMapType(G_HYBRID_MAP);

        myEventListener = GEvent.addListener(gmap,"click", function(overlay, latlng) {
            if (latlng) {
                gmap.openInfoWindow(latlng, myHtml);
                var lat = latlng.lat();
                var lon = latlng.lng();
                document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:latitude").value = lat;
                document.getElementById("Fragment_pai_GMap:Fragment_centro_GMap:form1:longitude").value = lon;
                var point = new GLatLng(lat, lon);
                var marca = new GMarker(point);
                gmap.addOverlay(marca);
                var myHtml = "Ultimo ponto selecionado.";
                gmap.openInfoWindow(latlng, myHtml);
            }
        });
    }else{
        alert("executei removelistener");
        GEvent.removeListener(myEventListener);
    }
}

//function doSaveAs(){
//    if (document.execCommand){
//        document.execCommand("SaveAs",true,"Nome arquivo");
//    }else{
//        alert('esta ação é suportada apenas pelo Internet Exlorer 4.0 ou superior.');
//    }
//
//}
var executeit = "n";
function exec(){
    if (executeit == "n"){ 
        //   if (gmap == null){
        executeit = "s";
        gmap = new GMap2(document.getElementById("formg:Frag_GMap:gmap"));
        geocoder = new GClientGeocoder();
        //   var latit = document.getElementById("formg:Frag_GMap:novaLat").value;
        //   var longit = document.getElementById("formg:Frag_GMap:novoLong").value;
        if ((latitudef == "null") || (latitudef == "") || (latitudef == 0)){
            geocoder.getLatLng(
                enderecof,
                function(point) {
                    if (!point) {
                        alert("Endereço " + enderecof + " não foi encontrado!");
                    } else {
                        gmap.setCenter(point, 16);
                        var marker = new GMarker(point);                        
                        gmap.addOverlay(marker);
                        marker.openInfoWindowHtml(enderecof);
                        gmap.setMapType(G_NORMAL_MAP);
                    }
                }
                );
        }else{
            var ponto = new GLatLng(latitudef, longitudef);
            gmap.setCenter(ponto, 17);
            gmap.setMapType(G_HYBRID_MAP);
            var marker = new GMarker(ponto);
            gmap.addOverlay(marker);
            marker.openInfoWindowHtml("Geo Localização <br/>" + enderecof);
        }
        gmap.addControl(new GLargeMapControl());
        gmap.addControl(new GMapTypeControl());
    }
//}
}

function clicked(overlay, latlng) {
    if (latlng) {
        geocoder.getLocations(latlng, function(addresses) {
            if(addresses.Status.code != 200) {
                alert("Não foi encontrado informações do ponto selecionado! " + latlng.toUrlValue());
            }
            else {
                address1 = addresses.Placemark[0];
                var myHtml = address1.address;
                gmap.openInfoWindow(latlng, myHtml);
            }
        });
    }
}

//var aSoundObject = soundManager.createSound({
// id:'mySound2',
// url:'resources/mensagem.mp3'
//});






